/*

Stickman Adventure - a game created by Daco & his cousin

Copyright (C) 2011-2012  DacoTaco & Wouterke

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation version 2.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


*/

#include "Screen.h" 
#include "Object_collection.h"

#ifdef OGL_MODE
#include "SDL.h"
#include "SDL_opengl.h"
#endif

extern _Object_collection Collector;

Screen::Screen(int xpos , int ypos , int h , int w ,int bitperpixel) : Object(NULL ,xpos ,ypos ,h , w ,OBJECT_SCREEN,NULL,NULL)
{
	//sheet from object is used as the screen
	bpp=bitperpixel;
#ifndef OGL_MODE
	sheet = SDL_SetVideoMode( w, h,bpp, SDL_HWSURFACE | SDL_RESIZABLE | SDL_DOUBLEBUF );//| SDL_FULLSCREEN );
	if(sheet == NULL)
	{
		//TODO : add error box
		exit(5);
	}
#else
		sheet = SDL_SetVideoMode( w, h, bpp, SDL_GL_DOUBLEBUFFER | SDL_OPENGL );
		glClearColor( 255, 255, 255, 0 );
		glClearDepth(1.0f);
		glViewport(0, 0, w, h);

		//Set projection
		glMatrixMode( GL_PROJECTION );
		glLoadIdentity();
		glOrtho( 0, w, h, 0, -1, 1 );

		//Initialize modelview matrix
		SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
		SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
		SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
		SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
		SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); 
		glMatrixMode( GL_MODELVIEW );
		glEnable(GL_TEXTURE_2D);
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

		//glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		//glColor4f(1, 1, 1, 0);
		//glBlendFunc(GL_SRC_ALPHA, GL_SRC_COLOR);
	
		glLoadIdentity();


		//If there was any errors
		if( glGetError() != GL_NO_ERROR )
		{
			printf("ffffuuu %x\n",glGetError());
			exit(5);
			return;
		}

#endif
	/*if (0 == (sheet->flags & SDL_HWSURFACE) ){
		gprintf("Can't get hardware surface 0x%x\n",sheet->flags);
	}
	const SDL_VideoInfo* info = NULL;

	info = SDL_GetVideoInfo( );
	gprintf("\nhw avail:%d\n",info->hw_available);
	gprintf("hw blit avail: %d\n",info->blit_hw);*/
	//Set the window caption 
	SDL_WM_SetCaption( "STICKMAN - CAN'T BREATH - Average Frames Per Second: 00.00", NULL );
}

int Screen::getBPP()
{
	return bpp;
}

//moves the screen along with a certain object
void Screen::moveScreen(Object* o)
{
	if(o != NULL)
	{
		y=(o->getYPosition()+o->getHeight()/2)-(height/2);
		x=(o->getXPosition()+o->getWidth()/2)-(width/2);
	}
}
//redraws the shits
char Screen::Update(int collision, int y_ground, int x_wall)
{
	moveScreen(Collector.GetPlayer(1));
#ifndef OGL_MODE
	SDL_Flip(sheet);
	//SDL_FillRect(sheet,GetColRect(),0xffffffff);
	//SDL_UpdateRects(sheet,1,&sheet->clip_rect);
#else
	SDL_GL_SwapBuffers();
#endif
	return 0;
}

void Screen::CollisionAction(Object* o, int collision)
{
	if(sheet != NULL)
	{
		//if(o->getType()==OBJECT_PLAYER)moveScreen(o);
		o->draw(sheet,this);
	}
}

Screen::~Screen()
{

}
SDL_Rect* Screen::getImageClip()
{
	clip.x=x;
	clip.y=y;
	clip.w=width;
	clip.h=height;
	return &clip;

}